package test;

import java.util.concurrent.Semaphore;

public class demo13 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(4);
        for(int i = 0; i < 20; i++){
            Thread t = new Thread(()-> {
                try {
                    System.out.println("申请资源");
                    semaphore.acquire();
                    System.out.println("获取到资源了");
                    Thread.sleep(1000);
                    semaphore.release();
                    System.out.println("释放资源");

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }
}

